package com.cndw.kungfu.model.battle;

import java.util.Collection;
import java.util.List;

import com.cndw.kungfu.config.Constants;
import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.config.ConstantsDayIntKey;
import com.cndw.kungfu.config.GoodsEffectConfig;
import com.cndw.kungfu.domain.GoodsFall;
import com.cndw.kungfu.domain.GoodsMode;
import com.cndw.kungfu.domain.Monster;
import com.cndw.kungfu.domain.Player;
import com.cndw.kungfu.ext.NumberProcesser;
import com.cndw.kungfu.model.ActivitySerialize;
import com.cndw.kungfu.model.Context;
import com.cndw.kungfu.model.DataStaticDay;

public class BattleOnDead {

	public static void FailByFighterList(Battle battle, Collection<Fighter> fighters) {

		int asset = 0;
		int exp = 0;
		int lastMonsterId = 0;

		for (Fighter e : fighters) {
			if (e instanceof FighterMonster) {
				Monster monster = (Monster) e.getSourceObject();
				battle.addDeadMonsterId(monster.getId());

				boolean ishavedGive = lastMonsterId == monster.getId();
				lastMonsterId = monster.getId();
				if (battle.getConfigType() == 1 && battle.getConfigNum() > 15) {
					// 10次之后没几率掉落
				} else {
					deadFailGoods(monster, battle, ishavedGive);
				}

				if (NumberProcesser.isWinning(monster.getAssetR())) {
					asset += monster.getAsset();
				}
				exp += monster.getExp();

				if (battle.getConfigType() == 1) {
					Monster monster2 = Context.getUndercityServer().getMonster(monster.getId());
					float xxf = battle.getSourceDam() * 10000f / monster2.getHp();
					int xxx = NumberProcesser.between(Float.valueOf(xxf).intValue(), 1, 10);
					asset = NumberProcesser.percentOnlyAddValue(asset, xxx) / 10 / (10 + battle.getConfigNum());
					exp = NumberProcesser.percentOnlyAddValue(exp, xxx) / 10 / (10 + battle.getConfigNum());
				}
			}
		}
		if (asset > 0 || exp > 0) {
			clacAssetAndExp(asset, exp, battle);
		}

	}

	private static void clacAssetAndExp(int asset, int exp, Battle battle) {

		int finalAsset = 0;
		int finalExp = 0;
		int trueAssetRate = NumberProcesser.mtRand(90, 110);
		int memberNums = battle.getAllPlayers().size();
		int xRate = 0;
		if (memberNums == 2) {// 组队有加成
			xRate = 20;
		} else if (memberNums == 3) {
			xRate = 50;
		}
		for (Player player : battle.getAllPlayers()) {
			int tmpExp = exp;
			int percentExp = 100 + xRate;
			int percentAsset = trueAssetRate;

			if (player.getKnight() != null) {
				percentExp += player.getKnight().getPveExpR();
			}
			if (player.getUnionId() > 0 && (player.getUnionId() == ActivitySerialize.getInstance().getSiegeUnionId()[0])) {
				percentExp += 20;
				percentAsset += 20;
			}

			if (battle.getSceneType() == Constants.CITY_WALL) {// 塞外打怪
				// 有这个属性的人一定是有效的加成
				if (player.getDataInfo().getExpCoef() > 0) {
					percentExp += player.getDataInfo().getExpCoef();
				}
			} else if (battle.getSceneType() == Constants.CITY_FIELD) {
				int battleNum = player.getDataInfo().getEveryDay().getBattleNum();
				if (battleNum <= 10800) {

				} else if (battleNum <= 16200) {
					percentExp = percentExp / 2;
				} else if (battleNum <= 21600) {
					percentExp = percentExp / 5;
				} else {
					percentExp = percentExp / 10;
				}
			}
			if (ConstantsConfig.isSysInspectAddicted() && !player.getDataInfo().isPassAddicted()) {
				int timeDiff = player.getTodayOnLine();
				if (timeDiff >= 5 * 3600) {
					percentExp = 0;
					percentAsset = 0;
				} else if (timeDiff >= 3 * 3600) {
					percentExp = percentExp / 2;
					percentAsset = percentAsset / 2;
				}
			}
			if (DataStaticDay.getInstance().isLoser(player.getLevel())) {
				percentExp = percentExp * 3 / 2;
			}
			finalExp += NumberProcesser.percentOnlyAddValue(tmpExp, percentExp);
			finalAsset += NumberProcesser.percentOnlyAddValue(asset, percentAsset);
		}
		finalExp = finalExp / memberNums;
		finalAsset = finalAsset / memberNums;
		battle.setAssetAndExp(finalAsset, finalExp);
	}

	/**
	 * 怪物掉落物品
	 * 
	 * @param monster
	 */
	private static void deadFailGoods(Monster monster, Battle battle, boolean ishavedGive) {

		List<GoodsFall> goodsFalls = monster.getItemFallDataList();
		if (goodsFalls == null || goodsFalls.isEmpty()) {
			return;
		}
		int vocationId = 0;
		if (monster.getId() == 423) {
			Object sourceObject = battle.getSourceMaster().getSourceObject();
			if (sourceObject instanceof Player) {
				if (!((Player) sourceObject).getDataInfo().isPassUnder(801)) {
					vocationId = ((Player) sourceObject).getVocationId();
				}
			}
		}
		int fallMax = monster.getItemFallMax();
		if (battle.isRateCoef()) {
			fallMax *= 2;
		}
		for (int i = 0; i < fallMax; i++) {
			int currentRate = NumberProcesser.mtRand(1000);
			int goodsRate = 0;
			boolean isHaved = true;
			for (GoodsFall e : goodsFalls) {
				GoodsMode goodsMode = Context.getGoodsServer().getModeById(e.getGoodsId());
				if (goodsMode == null) {// 不存在的物品不发送
					continue;
				}
				if (vocationId > 0) {
					if (goodsMode.isEquiq() && goodsMode.getVocationId() != vocationId) {
						continue;
					} else {
						currentRate = 1;
						vocationId = 0;
					}
				}

				if (goodsMode.isTaskItem()) {
					if (i != 0 || ishavedGive) {
						continue;
					}
					for (Player player : battle.getAllPlayers()) {
						if (Context.getTaskServer().checkNeedFail(player.getPlayerId(), e.getTaskId(), e.getGoodsId())) {
							// System.out.println(ishavedGive);
							battle.addFailGoods(monster.getUniqueId(), e.getGoodsId(), player.getPlayerId());
						}
					}
				} else if (isHaved) {
					goodsRate += e.getRate();
					if (currentRate > goodsRate + battle.failLuckAdd) {
						continue;
					}
					battle.addFailGoods(monster.getUniqueId(), e.getGoodsId(), 0, e.getBind());
					isHaved = false;
					if (i != 0) {
						break;
					}
				}
			}
		}

		if (!ishavedGive && monster.getId() < 0) {
			int rateNum = battle.getSceneType() == Constants.CITY_FIELD ? 1 : 6;
			// rateNum = 100;
			for (Player e : battle.getAllPlayers()) {
				boolean canGot = false;
				if (battle.getSceneType() == Constants.CITY_FIELD) {
					canGot = e.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.zzB1) < 3;
				} else if (battle.getSceneType() == Constants.CITY_UNDER || battle.getSceneType() == Constants.CITY_WALL
						|| battle.getSceneType() == Constants.CITY_FAM) {
					canGot = e.getDataInfo().getEveryDay().intDataGet(ConstantsDayIntKey.zzB2) < 5;
				}
				if (canGot && NumberProcesser.isWinning(rateNum)) {// 万圣节放变身卡
					battle.addFailGoods(monster.getUniqueId(), GoodsEffectConfig.guaiwuka, e.getPlayerId(), 1);
					if (battle.getSceneType() == Constants.CITY_FIELD) {
						e.getDataInfo().getEveryDay().intDataAlter(ConstantsDayIntKey.zzB1, 1);
					} else {
						e.getDataInfo().getEveryDay().intDataAlter(ConstantsDayIntKey.zzB2, 1);
					}
				}
			}
		}

	}

}
